package com.devmindset.legoframework.usage.client.component;

import java.util.List;

import com.devmindset.legoframework.client.mvc.LegoComponent;
import com.devmindset.legoframework.client.mvc.LegoModel;
import com.devmindset.legoframework.client.mvc.metadata.ComponentStory;
import com.devmindset.legoframework.client.mvc.metadata.Stories;
import com.devmindset.legoframework.usage.client.datasource.CountryLocalDS;
import com.devmindset.legoframework.usage.client.event.Events;
import com.devmindset.legoframework.usage.client.records.CountryRecord;
import com.devmindset.legoframework.usage.client.text.UsageTexts;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.viewer.DetailViewer;

public class CountryDetailsComponent extends LegoComponent {

	private CountryLocalDS dataSource;
	
	@Override
	public Canvas construct(LegoModel model) {

		dataSource = CountryLocalDS.getInstance();
		SectionStack printStack = new SectionStack();
		printStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		printStack.setWidth100();
		printStack.setHeight100();

		final DetailViewer printViewer = new DetailViewer();
		printViewer.setDataSource(dataSource);
		printViewer.setWidth100();
		printViewer.setMargin(15);
		printViewer.setEmptyMessage(UsageTexts.get().selectCountryMsg());

		final ListGrid printGrid = new ListGrid();
		printGrid.setHeight100();

		printGrid.setDataSource(dataSource);

		ListGridField countryCode = new ListGridField("countryCode", UsageTexts.get().code(), 50);
		ListGridField countryName = new ListGridField("countryName", UsageTexts.get().country());
		ListGridField capital = new ListGridField("capital", UsageTexts.get().capital());
		ListGridField continent = new ListGridField("continent", UsageTexts.get().continent());
		printGrid.setFields(countryCode, countryName, capital, continent);
		
		printGrid.setAlternateRecordStyles(true);

		printGrid.addRecordClickHandler(new RecordClickHandler() {
			public void onRecordClick(RecordClickEvent event) {
				printViewer.setData(new Record[] { event.getRecord() });
			}
		});

		SectionStackSection countriesSection = new SectionStackSection(UsageTexts.get().countries());
		countriesSection.setExpanded(true);
		countriesSection.addItem(printGrid);
		printStack.addSection(countriesSection);

		SectionStackSection detailsSection = new SectionStackSection(UsageTexts.get().countryDetails());
		detailsSection.setExpanded(true);
		detailsSection.addItem(printViewer);
		printStack.addSection(detailsSection);

		final VLayout printContainer = new VLayout(10);

		HLayout printButtonLayout = new HLayout(5);

		IButton newButton = new IButton(UsageTexts.get().newLabel());
		newButton.setDisabled(true);
		IButton changeButton = new IButton(UsageTexts.get().change());
		changeButton.setDisabled(true);
		IButton printPreviewButton = new IButton(UsageTexts.get().printPreview());
		printPreviewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Canvas.showPrintPreview(printContainer);
			}
		});
		printButtonLayout.addMember(newButton);
		printButtonLayout.addMember(changeButton);
		printButtonLayout.addMember(printPreviewButton);
		printButtonLayout.setPadding(7);

		printContainer.addMember(printStack);
		printContainer.addMember(printButtonLayout);

		// The filter is just to limit the number of records in the ListGrid - we
		// don't want to print them all
		printGrid.filterData(new Criteria("CountryName", "land"), new DSCallback() {
			public void execute(DSResponse response, Object rawData, DSRequest request) {
				printGrid.selectRecord(0);
				printViewer.setData(new Record[] { printGrid.getSelectedRecord() });
			}
		});

		return printContainer;
	}

	@Override
	public void calculate(LegoModel model) {
		// here's the place to recalculate your data source with updated info.
		List<CountryRecord> data = model.getModelData();
		dataSource.setTestData(data.toArray(new CountryRecord[] {}));
		dataSource.fetchData();
	}

	public ComponentStory myStory() {
		return Stories.component()
			.constructedBy(Events.RENDER_COUNTRY_DETAILS_COMPONENT)
			.knownAs("Country Details Component");
	}

}